package com.ys.tdm.util;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;

/**
 *@Title: 
 *@Description: 上传文件工具类
 *@Author: T.J
 *@Since: 2014年5月5日
 *@Version: 1.1.0
 *@Copyright: Copyright (c) ########有限公司 2014 ~ 2015 版权所有
 */
public class UploadUtils {
	/**
	 * 日期格式化对象
	 */
	public static final DateFormat MONTH_FORMAT = new SimpleDateFormat(
			"/yyyyMM/ddHHmmss");
	
	public static final DateFormat YEAR_MONTH_FORMAT = new SimpleDateFormat("yyyyMM");
	public static final DateFormat YEAR_MONTH_DAY_FORMAT = new SimpleDateFormat("yy/MM/dd");
	
	public static final DateFormat SHORT_FORMAT = new SimpleDateFormat("ddHHmmss");
	
	public static String generateMonthname() {
		return YEAR_MONTH_FORMAT.format(new Date());
	}

	
	
	public static String generateInvoiceFileName(String path, String fileName, String ext){
		return path + YEAR_MONTH_DAY_FORMAT.format(new Date()) + "/" + fileName + "." + ext;
	}
	
	public static String generateByFilename(String path,String fileName, String ext) {
		return path + fileName + "." + ext;
	}

	protected static final Pattern ILLEGAL_CURRENT_FOLDER_PATTERN = Pattern
			.compile("^[^/]|[^/]$|/\\.{1,2}|\\\\|\\||:|\\?|\\*|\"|<|>|\\p{Cntrl}");

	public static String sanitizeFileName(final String filename) {

		if (StringUtils.isBlank(filename))
			return filename;

		String name = forceSingleExtension(filename);

		// Remove \ / | : ? * " < > 'Control Chars' with _
		return name.replaceAll("\\\\|/|\\||:|\\?|\\*|\"|<|>|\\p{Cntrl}", "_");
	}

	public static String sanitizeFolderName(final String folderName) {

		if (StringUtils.isBlank(folderName))
			return folderName;

		// Remove . \ / | : ? * " < > 'Control Chars' with _
		return folderName.replaceAll(
				"\\.|\\\\|/|\\||:|\\?|\\*|\"|<|>|\\p{Cntrl}", "_");
	}

	public static boolean isValidPath(final String path) {
		if (StringUtils.isBlank(path))
			return false;

		if (ILLEGAL_CURRENT_FOLDER_PATTERN.matcher(path).find())
			return false;

		return true;
	}

	public static String forceSingleExtension(final String filename) {
		return filename.replaceAll("\\.(?![^.]+$)", "_");
	}

	public static boolean isSingleExtension(final String filename) {
		return filename.matches("[^\\.]+\\.[^\\.]+");
	}

	public static void checkDirAndCreate(File dir) {
		if (!dir.exists())
			dir.mkdirs();
	}

	public static File getUniqueFile(final File file) {
		if (!file.exists())
			return file;

		File tmpFile = new File(file.getAbsolutePath());
		File parentDir = tmpFile.getParentFile();
		int count = 1;
		String extension = FilenameUtils.getExtension(tmpFile.getName());
		String baseName = FilenameUtils.getBaseName(tmpFile.getName());
		do {
			tmpFile = new File(parentDir, baseName + "(" + count++ + ")."
					+ extension);
		} while (tmpFile.exists());
		return tmpFile;
	}
	
	/**
	 * @param filename
	 * @return
	 * @Description: get file extension
	 */
	public static String getExtension(String filename) {
		return getExtension(filename, "");
	}
	
	/**
	 * @param filename
	 * @param defExt 
	 * @return
	 * @Description: get file extension
	 */
	public static String getExtension(String filename, String defExt) {
		if ((filename != null) && (filename.length() > 0)) {
			int i = filename.lastIndexOf('.');
			if ((i >-1) && (i < (filename.length() - 1))) {
				return filename.substring(i + 1);
			}
		}
		return defExt;
	} 
	
	
}
